<?php
namespace App\Service\Admin;

class TongjiService20250921 extends BaseService
{
    //统计

    //个人
    public static function person($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYwWhItem)->where('del', 0);
        if ($pas['uid']) {
            $db = $db->where('uid', $pas['uid']);
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据');
        }
        $arys = [];
        foreach ($res as $v) {
            $item_title = '?';
            $wh_title = '?';
            $configOne = self::dbConn(self::$dbConfigWorkHour)->where('id', $v->config_whid)->first();
            if ($configOne) {
                $wh_title = $configOne->title;
            }
            $whOne = self::dbConn(self::$dbYwWh)->where('id', $v->wh_id)->first();
            if ($whOne && $whOne->item_id) {
                $itemOne = self::dbConn(self::$dbConfigItem)->where('id', $whOne->item_id)->first();
                if (!empty($itemOne)) $item_title = $itemOne->title;
            }
            $resDelay = self::getDelay($v->starthour, $v->endhour);
            $delay_str = $resDelay['code']==0 ? $resDelay['data']['str'] : '';
            $arys[] = [
                'id' => $v->id, 'item_title' => $item_title,
                'wh_title' => $wh_title, 'title' => $v->title,
                'starthour' => $v->starthour, 'endhour' => $v->endhour,
                'delay_str' => $delay_str,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //部门
    public static function item($pages, $pas)
    {
        $leadOne = self::dbConn(self::$dbStaff)
            ->where('is_able', 0)
            ->where('depart_lead', 1)
            ->first();
        if (empty($leadOne)) {
            return self::toback(-2, '没有权限');
        }
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYwWhItem)
            ->where('del', 0)
            ->where('depart_id', $pas['depart_id']);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据');
        }
        $arys = [];
        foreach ($res as $v) {
            $staffOne = self::dbConn(self::$dbStaff)
                ->where('id', $v->uid)
                ->first();
            $staff_title = empty($staffOne) ? '?' : $staffOne->title;
            $item_title = '？';
            $wh_title = '？';
            $whOne = self::dbConn(self::$dbYwWh)->where('id', $v->wh_id)->first();
            if ($whOne && $whOne->item_id) {
                $itemOne = self::dbConn(self::$dbConfigItem)->where('id', $whOne->item_id)->first();
                if (!empty($itemOne)) $item_title = $itemOne->title;
            }
            $configOne = self::dbConn(self::$dbConfigWorkHour)->where('id', $v->config_whid)->first();
            if ($configOne) {
                $wh_title = $configOne->title;
            }
            $delay = 0;
            $itemOnes = self::dbConn(self::$dbYwWhItem)
                ->where('wh_id', $v->id)
                ->get();
            if (count($itemOnes)) {
                foreach ($itemOnes as $vv) {
                    $delay += $vv->delay;
                }
            }
            $arys[] = [
                'id' => $v->id, 'item_title' => $item_title,
                'wh_title' => $wh_title, 'title' => $v->title,
                'delay' => $delay, 'staff_title' => $staff_title,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //审核员
    public static function all($pages, $pas)
    {
        if ($pas['role_id'] != 5) {
            return self::toback(-2, '只有审核员有权限');
        }
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYwWhItem)->where('del', 0);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据');
        }
        $arys = [];
        foreach ($res as $v) {
            $staffOne = self::dbConn(self::$dbStaff)
                ->where('is_able', 0)
                ->where('depart_lead', 1)
                ->first();
            $staff_title = empty($staffOne) ? '?' : $staffOne->title;
            $item_title = '？';
            $wh_title = '？';
            $whOne = self::dbConn(self::$dbYwWh)->where('id', $v->wh_id)->first();
            if ($whOne && $whOne->item_id) {
                $itemOne = self::dbConn(self::$dbConfigItem)->where('id', $whOne->item_id)->first();
                if (!empty($itemOne)) $item_title = $itemOne->title;
            }
            $configOne = self::dbConn(self::$dbConfigWorkHour)->where('id', $v->config_whid)->first();
            if ($configOne) {
                $wh_title = $configOne->title;
            }
            $arys[] = [
                'id' => $v->id, 'staff_title' => $staff_title,
                'item_title' => $item_title, 'wh_title' => $wh_title,
                'title' => $v->title, 'delay' => $v->delay,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //环比
    public static function huan($pages, $pas)
    {
        $leadOne = self::dbConn(self::$dbStaff)
            ->where('is_able', 0)
            ->where('depart_id', $pas['depart_id'])
            ->where('depart_lead', 1)
            ->first();
        if (empty($leadOne)) {
            return self::toback(-2, '没有权限');
        }
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbStaff)
            ->where('is_able', 0)
            ->where('depart_id', $pas['depart_id'])
            ->where('depart_lead', 0);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据');
        }
        $arys = [];
        foreach ($res as $v) {
            $delay = self::dbConn(self::$dbYwWhItem)->where('uid', $v->id)->sum('delay');
            //同比，一年内
            $year0 = date('Y-m-d H:i:s', strtotime("-1 year"));
            $year1 = date('Y-m-d H:i:s', strtotime("-2 year"));
            $delay_year0 = self::dbConn(self::$dbYwWhItem)
                ->where('uid', $v->id)
                ->where('addtime', '>=', $year0)
                ->where('addtime', '<=', toDate())
                ->sum('delay');
            $delay_year1 = self::dbConn(self::$dbYwWhItem)
                ->where('uid', $v->id)
                ->where('addtime', '>=', $year1)
                ->where('addtime', '<=', $year0)
                ->sum('delay');
            $year = $delay_year1 ? floor($delay_year0 / $delay_year1) : 0;
            //环比，一周内
            $week0 = date('Y-m-d H:i:s', strtotime("-1 week"));
            $week1 = date('Y-m-d H:i:s', strtotime("-2 week"));
            $delay_week0 = self::dbConn(self::$dbYwWhItem)
                ->where('uid', $v->id)
                ->where('addtime', '>=', $week0)
                ->where('addtime', '<=', toDate())
                ->sum('delay');
            $delay_week1 = self::dbConn(self::$dbYwWhItem)
                ->where('uid', $v->id)
                ->where('addtime', '>=', $week1)
                ->where('addtime', '<=', $week0)
                ->sum('delay');
            $week = $delay_week1 ? floor($delay_week0 / $delay_week1) : 0;
            $arys[] = [
                'id' => $v->id, 'staff_title' => $v->title,
                'delay' => $delay, 'year' => $year, 'week' => $week,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //图
    public static function tu()
    {
        $leadOne = self::dbConn(self::$dbStaff)
            ->where('is_able', 0)
            ->where('depart_lead', 1)
            ->first();
        if (empty($leadOne)) {
            return self::toback(-2, '没有权限');
        }
        $arys = [
            'bing' => [['xxx', 10], ['zzz', 20]], //项目占比
            'zhu' => [ //工时占比
                'title' => ['张三', '李四'],
                'val' => [[10], [50]],
            ],
        ];
//        return self::toback(0, '', $arys);
        $itemOnes = self::dbConn(self::$dbConfigItem)
            ->where('del', 0)
            ->get();
        if (count($itemOnes)) {
            foreach ($itemOnes as $v) {
                $delay = self::dbConn(self::$dbYwWh)
                    ->where('del', 0)
                    ->where('item_id', 0)
                    ->sum('delay');
                $arys['bing'][] = [
                    $v->title, $delay,
                ];
            }
        }
        $staffOnes = self::dbConn(self::$dbStaff)
            ->where('is_able', 0)
            ->where('depart_lead', 0)
            ->get();
        if (count($staffOnes)) {
            foreach ($staffOnes as $v) {
                $delay = self::dbConn(self::$dbYwWhItem)
                    ->where('del', 0)
                    ->where('uid', 0)
                    ->sum('delay');
                $arys['zhu']['title'][] = $v->title;
                $arys['zhu']['val'][] = [$delay];
            }
        }
        return self::toback(0, '', $arys);
    }

    //选项
    public static function getOption()
    {
        $arys = ['staff' => []];
        $arys['staff'] = self::dbConn(self::$dbStaff)
            ->select('id', 'title')
            ->where('is_able', 0)
            ->get();
        return self::toback(0, '', $arys);
    }

    //获取员工月底数据
    public static function staffMonthList($staff_id,  $year, $month)
    {
        $staffOne = (array) self::dbConn(self::$dbStaff)
            ->where('is_able', 0)
            ->where('id', $staff_id)
            ->first();
        if (empty($staffOne)) {
            return self::toback(-2, '没有该员工');
        }
        $depart_id = $staffOne['depart_id'];
        $configWhOnes = self::dbConn(self::$dbConfigWorkHour)
            ->where('del', 0)
            ->where('depart_id', $depart_id)
            ->get();
        if (!count($configWhOnes)) {
            return self::toback(-2, '没有工时配置');
        }
        //标题数据
        $title[] = '员工';
        $title[] = '工时分配';
        $month = strlen($month)==1 ? '0'.$month : $month;
        $days = [];
        //$day_num = cal_days_in_month(CAL_GREGORIAN, $month, $year);
        $day_num = date('t', strtotime($year.'-'.$month.'-01'));
        for ($i = 1; $i <= $day_num; $i ++) {
            $title[] = $i;
            $days[] = $i;
        }
        $arys['data'][] = $title;
        foreach ($configWhOnes as $v) {
            $tlist = [$staffOne['title'], $v->title];
            $month_total = 0;
            foreach ($days as $day) {
                $day_str = strlen($day)==1 ? '0'.$day : $day;
                $daystr = $year.'-'.$month.'-'.$day_str;
                $daystart = $daystr.' 00:00:01';
                $dayend = $daystr.' 23:59:59';
                $delay = self::dbConn(self::$dbYwWhItem)
                    ->where('del', 0)
                    ->where('uid', $staff_id)
                    ->where('config_whid', $v->id)
                    ->where('uptime', '>=', $daystart)
                    ->where('uptime', '<=', $dayend)
                    ->sum('delay');
                $total = number_format($delay / 60, 1);
                $tlist[] = $total;
                $month_total += $total;
                $tlist[] = $month_total;
            }
            $arys['data'][] = $tlist;;
        }
//        $arys['data'][] = [
//            $staffOne['title'], '合计',
//        ];
        $arys['staff_name'] = $staffOne['title'];
        return self::toback(0, '', $arys);
    }
}